import requests
import time
import hashlib
import re
import json
from openpyxl import Workbook

def md5sum(str):
    # md5加密
    m = hashlib.md5()  # 创建一个hashlib.md5()对象
    m.update(str.encode("utf8"))  # 将参数转换为UTF8编码
    return m.hexdigest()  # 用十六进制输出加密后的数据

def get_mw_uuid():
    # 获取mw_uuid
    url_ = "https://portal.mogu.com/api/util/getUuid?callback=callback_1001"
    req2 = session.get(url_)
    cookies = req2.cookies.items()
    for name, value in cookies:
        mw_uuid = value
    return mw_uuid

def get_token():
    # 此处的和后续的token生成有关系最后的加的为{"pids":"132244,138852,138851"}加密和token
    e = "100028&pc-search-wall&unknown&{}&NMMain@mgj_pc_1.0&{}&mwp.pagani.search&" \
        "3&b0113484d22f2dbbfb073a607f269942&".format(int(time.time() * 1000), mw_uuid)

    # # 得到cookie __mgjuuid,获取另外的加密
    url = "https://api.mogu.com/h5/mwp.darwin.multiget/3/"
    # # 访问上面的url，进行cookie的获取，设置cookie

    params = {
        'data': '{"pids":"132244,138852,138851"}',
        'mw-appkey': 100028,
        'mw-ttid': 'NMMain@mgj_pc_1.0',
        'mw-t': int(time.time() * 1000),
        'mw-uuid': mw_uuid,
        'mw-h5-os': 'unknown',
        'mw-sign': md5sum(e),
        'callback': 'mwpCb1',
        '_': int(time.time() * 1000)
    }
    session.get(url, params=params)
    token = session.cookies['_mwp_h5_token']
    return token

def get_data(page):
    session.headers['referer'] = 'https://list.mogu.com/'
    url = "https://api.mogu.com/h5/mwp.pagani.search/19/"
    params = {
        'data': '{"page":1,"pageSize":24,"sort":"pop","ratio":"3:4","cKey":"pc-search-wall","q":"%E8%BF%9E%E8%A1%A3%E8%A3%99","from":"querytip0","ptp":"31.v5mL0b.0.0.cxy8RxIZ"}',
        'mw-ckey': 'pc-search-wall',
        'mw-appkey': 100028,
        'mw-ttid': 'NMMain@mgj_pc_1.0',
        'mw-t': int(time.time() * 1000),
        'mw-uuid': mw_uuid,
        'mw-h5-os': 'unknown',
        # 'mw-sign': md5sum(e),
        'callback': 'mwpCb3',
        '_': int(time.time() * 1000)
    }
    data = json.loads(params['data'])
    data['page'] = page
    params['data'] = json.dumps(data).replace(" ","")
    e = "100028&pc-search-wall&unknown&{}&NMMain@mgj_pc_1.0&{}&mwp.pagani.search&" \
        "19&{}&{}".format(int(time.time() * 1000), mw_uuid, md5sum(params['data']), token)

    params['mw-sign'] = md5sum(e)
    resp = session.get(url, params=params)
    return resp.text

def deal_data(res):
    pattern = re.compile('mwpCb3\((.*?)}\)')
    data = pattern.findall(res)
    json_data = json.loads(data[0] +'}')
    docs = json_data['data']['wall']['docs']
    items = []
    for doc in docs:
        item = []
        item.append(doc['title'])
        item.append(doc['price'])
        item.append(doc['img'])
        item.append(doc['link'])
        items.append(item)
    return items

def save_xls(info_list):
    wb = Workbook()
    ws1 = wb.active
    ws1.title = "连衣裙"
    ws1.append(["title", "price", "image", "url"])
    for rows in info_list:
        ws1.append(rows)
    wb.save('{}商品信息.xlsx'.format("连衣裙"))

if __name__ == '__main__':
    headers = {
        'referer': 'https://www.mogu.com/',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
    }
    session = requests.session()
    session.headers = headers
    mw_uuid = get_mw_uuid()
    token = get_token()
    info_lists = []
    for page in range(1,5):
        res = get_data(page)
        info_list = deal_data(res)
        info_lists += info_list
    save_xls(info_lists)